package com.taxinfo.service.impl.data;

import com.taxinfo.pojo.dto.AccBalMapInfoDto;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;

public class CommFunction {

    /**
     * 所属期获得
     * @param sblx
     * @param sbYear
     * @return
     */
    protected String[] getSsqInfo(String sblx, String sbYear) {
        String[] ssqInfo = new String[2];
        if("年度".equals(sblx)){
            ssqInfo[0] = sbYear.substring(0,4).concat("0101");
            ssqInfo[1] = sbYear.substring(0,4).concat("1231");
        }else if("季度".equals(sblx)){
            int quarter = Integer.parseInt(sbYear.substring(5,6));
            int startMonth = (quarter - 1) * 3 + 1;
            int endMonth = quarter * 3;
            if(endMonth > 12){
                endMonth = 12;
            }
            String ssYear = sbYear.substring(0,4);
            ssqInfo[0] = ssYear.concat(String.format("%02d", startMonth)).concat("01");
            if(endMonth==6 || endMonth == 9){
                ssqInfo[1] = ssYear.concat(String.format("%02d", endMonth)).concat("30");
            }else{
                ssqInfo[1] = ssYear.concat(String.format("%02d", endMonth)).concat("31");
            }
        }else if("月度".equals(sblx)){
            String year = sbYear.substring(0,4);
            String month = sbYear.substring(5,sbYear.length()-1);
            ssqInfo[0] = year.concat(String.format("%02d",Integer.parseInt(month))).concat("01");
            if(("2").equals(month)){
                if(Integer.parseInt(year)%4==0 ){
                    ssqInfo[1] = year.concat("02").concat("29");
                }else{
                    ssqInfo[1] = year.concat("02").concat("28");
                }
            }else if("1".equals(month) || "3".equals(month) || "5".equals(month) || "7".equals(month) || "8".equals(month) || "10".equals(month) || "12".equals(month)){
                ssqInfo[1] = year.concat(String.format("%02d",Integer.parseInt(month))).concat("31");
            }else{
                ssqInfo[1] = year.concat(String.format("%02d",Integer.parseInt(month))).concat("30");
            }
        }
        return ssqInfo;
    }

    /**
     * 核对是否包含所需的科目项
     * @param standardSubMap
     * @param items
     * @return
     */
    protected String checkItem(Map<String, AccBalMapInfoDto> standardSubMap, String[] items) {
        for(String item : items){
            if(!standardSubMap.containsKey(item)){
                System.out.println("###项目不存在= ".concat(item));
                return item;
            }
        }
        return "";
    }

    /**
     *
     * @param decimal
     * @param subType
     * @param accBalType
     * @return
     */
    public BigDecimal getAmtDecimal(BigDecimal decimal, String subType, String accBalType) {
        if(accBalType.contains("平") || null == decimal){
            return BigDecimal.ZERO;
        }else if(subType.contains("负债权益类") || subType.contains("收入类")){
            if(accBalType.contains("贷")){
                return decimal;
            }else if(accBalType.contains("借")){
                return decimal.negate();
            }
        }else if(subType.contains("资产类") || subType.contains("成本费用类") || subType.contains("生产成本类")){
            if(accBalType.contains("借")){
                return decimal;
            }else if(accBalType.contains("贷")){
                return decimal.negate();
            }
        }else{
            return decimal;
        }
        return decimal;
    }

    /**
     * type，0：字符，1：数字
     * @param obj
     * @param type
     * @return
     */
    protected BigDecimal getAmtDecimal(BigDecimal decimal) {
        if (null == decimal){
            return BigDecimal.ZERO;
        }else{
            return decimal;
        }
    }

    /**
     * type，0：字符，1：数字
     * @param obj
     * @param type
     * @return
     */
    protected String getString(Object obj,String type) {
        if (null == obj && "0".equals(type)) {
            return "";
        }else{
            return String.valueOf(null== obj ? "0" : obj);
        }
    }

    /**
     * 获取单元格信息
     * @param cell
     * @return
     */
    protected String getCellInfo(Cell cell) {
        String cellValue = "";
        if (cell.getCellType() == CellType.STRING) {
            cellValue = cell.getStringCellValue();
        }else if (cell.getCellType() == CellType.NUMERIC) {
            // 将科学计数法转换为常规数字格式
            double value = cell.getNumericCellValue();
            cellValue = new DecimalFormat("###.##").format(value); // 格式化为常规数字格式
        }else{
            ;
        }
        return cellValue;
    }
}
